﻿Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading

Public Class Utilisateur
    Private m_UserLevel As UserLevel
    Private m_Sexe As Sexe
    Private m_Id As String
    Private m_Compte As String
    Private m_Pseudo As String
    Private m_Age As Integer
    Private m_Ville As String
    Private m_Domain As Integer

    Private m_client As TcpClient
    Private m_stream As NetworkStream

    Private m_lock As New Object()

    Private m_State As Integer = 0

    Public ReadOnly Property UserLevel As UserLevel
        Get
            Return m_UserLevel
        End Get
    End Property

    Public ReadOnly Property Sexe As Sexe
        Get
            Return m_Sexe
        End Get
    End Property

    Public ReadOnly Property Id As String
        Get
            Return m_Id
        End Get
    End Property

    Public ReadOnly Property Compte As String
        Get
            Return m_Compte
        End Get
    End Property

    Public ReadOnly Property Pseudo As String
        Get
            Return m_Pseudo
        End Get
    End Property

    Public ReadOnly Property Age As Integer
        Get
            Return m_Age
        End Get
    End Property

    Public ReadOnly Property Ville As String
        Get
            Return m_Ville
        End Get
    End Property

    Public ReadOnly Property Domain As Integer
        Get
            Return m_Domain
        End Get
    End Property

    Public Property Status As String = "en ligne"

    Public ReadOnly Property IP As IPAddress
        Get
            Return Nothing
        End Get
    End Property

    Public ReadOnly Property Host As String
        Get
            Return Nothing
        End Get
    End Property

    Public Property IsNoPV As Boolean
        Get
            Return (m_State And 2) <> 0
        End Get
        Set(value As Boolean)
            If value Then
                m_State = m_State Or 2
            Else
                m_State = m_State And 253
            End If
        End Set
    End Property

    Public Property IsBusy As Boolean
        Get
            Return (m_State And 64) <> 0
        End Get
        Set(value As Boolean)
            If value Then
                m_State = m_State Or 64
            Else
                m_State = m_State And 191
            End If
        End Set
    End Property

    Public Property IsInvisible As Boolean
        Get
            Return If(UserLevel = JuneLiveServer.UserLevel.Utilisateur, False, (m_State And 4) <> 0)
        End Get
        Set(value As Boolean)
            If value = False OrElse UserLevel = JuneLiveServer.UserLevel.Utilisateur Then
                m_State = m_State And 251
            Else
                m_State = m_State Or 4
            End If
        End Set
    End Property

    Public Property State As Integer
        Get
            Return m_State
        End Get
        Set(value As Integer)
            m_State = (value And 255)
            If IsInvisible And UserLevel = JuneLiveServer.UserLevel.Utilisateur Then
                IsInvisible = False
            End If
        End Set
    End Property

    Public Property SalonCourant As Salon

    Public Sub New(ByRef client As TcpClient, utilisateurPreco As UtilisateurPreco)
        Dim now = DateTime.Now

        With Me
            .m_client = client
            .m_stream = client.GetStream()

            .m_Id = (
                now.Day.ToString & now.Hour.ToString() & ":" &
                now.Month.ToString() & now.Minute.ToString() & now.Second.ToString()
            ).Replace("/", ":").Replace(" ", "") & ":" &
                DateTime.Now.Millisecond.ToString() & ":" & utilisateurPreco.Domain
            'utilisateurPreco.Sexe

            .m_Compte = utilisateurPreco.Compte
            .m_Pseudo = utilisateurPreco.Pseudo
            .m_UserLevel = utilisateurPreco.UserLevel
            .m_Age = utilisateurPreco.Age
            .m_Sexe = utilisateurPreco.Sexe
            .m_Ville = utilisateurPreco.Ville
            .m_Domain = utilisateurPreco.Domain
        End With
    End Sub

    Public Sub SendData(data() As String)
        If data.Count = 0 Then Exit Sub

        Dim fdata As String = Microsoft.VisualBasic.Join(data, vbTab)
        Console.WriteLine("SENDING(" & m_Pseudo & ")=[" & fdata.Replace(vbTab, "|") & "]")
        Logs.Access("SENDING(" & m_Pseudo & ")=[" & fdata.Replace(vbTab, "|") & "]")

        SendDataBytes(Encoding.UTF8.GetBytes(fdata & Chr(10)))
    End Sub

    Public Sub SendData(data()() As String)
        Dim sdata As String = ""

        For Each tdata In data
            sdata &= Microsoft.VisualBasic.Join(tdata, vbTab) & Chr(10)
        Next

        Dim lsdata = sdata.Replace(vbTab, "|")
        Console.WriteLine("SENDING(" & m_Pseudo & ")=[" & lsdata & "]")
        Logs.Access("SENDING(" & m_Pseudo & ")=[" & lsdata & "]")

        SendDataBytes(Encoding.UTF8.GetBytes(sdata))
    End Sub

    Public Sub SendData(data As List(Of String))
        SendData(data.ToArray)
    End Sub

    Public Sub SendDataBytes(data() As Byte)
        Try
            SyncLock m_lock
                'IO.File.AppendAllText("network.log", "[" & DateTime.Now.ToString() & "] /*   SEND   */ " & fdata.Replace(vbTab, "|"))
                m_stream.Write(data, 0, data.Length)
            End SyncLock
        Catch ex As Exception
            m_stream.Close()
            m_client.Close()
            Debug.Print("Failed to write stream (" & ex.Message & ")")
            Logs.Error("Failed to write stream (" & ex.Message & ")")
        End Try
    End Sub

    Public Sub Disconnect()
        If Not IsNothing(m_client) AndAlso m_client.Connected Then
            m_client.Close()
        End If

        If Not IsNothing(m_stream) Then
            m_stream.Close()
        End If
    End Sub
End Class
